﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Tododoo.Lib
{
    public static class Windower
    {
        private static Dictionary<Type, Type> pairs = new Dictionary<Type, Type>();

        public static void Register<TViewModel, TView>() where TView : FrameworkElement, new()
        {
            if (pairs.ContainsKey(typeof(TViewModel))) { pairs.Remove(typeof(TViewModel)); }
            pairs.Add(typeof(TViewModel), typeof(TView));
        }

        public static void Show(ViewModelBase viewModel, int? width, int? height, bool makeItBorderless)
        {
            Show(viewModel, width, height, makeItBorderless, false);
        }

        public static void Show(ViewModelBase viewModel, int? width, int? height, bool makeItBorderless, bool centerOnScreen)
        {
            Window window = new Window();

            if (width == null && height == null)
            {
                window.SizeToContent = SizeToContent.WidthAndHeight;
            }
            else if (width.HasValue && height == null)
            {
                window.SizeToContent = SizeToContent.Height;
                window.Width = width.Value;
            }
            else if (height.HasValue && window == null)
            {
                window.SizeToContent = SizeToContent.Width;
                window.Height = height.Value;
            }
            else
            {
                window.Width = width.Value;
                window.Height = height.Value;
            }

            if (makeItBorderless)
            {
                window.WindowStyle = WindowStyle.None;
                window.ShowInTaskbar = false;
                window.AllowsTransparency = true;
                window.Background = Brushes.Transparent;
            }

            if (centerOnScreen)
            {
                window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            }

            viewModel.RequestClose += (s, e) => window.Close();

            window.Title = viewModel.Title;
            window.Content = viewModel;
            window.UseLayoutRounding = true;
            TextOptions.SetTextFormattingMode(window, TextFormattingMode.Display);
            window.ShowDialog();
        }
    }
}
